﻿using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace DFGK_WMS.WebAPI.Controllers.Dictionary
{
    /// <summary>
    /// 基础数据管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class DictionnaryController : ControllerBase
    {

        private readonly ISqlSugarClient _Client;

        public DictionnaryController(ISqlSugarClient client)
        {
            _Client = client;
        }


        /// <summary>
        /// 获取所有公司列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetAllComapy")]
        public IActionResult GetAllComapy()
        {
            var result = _Client.Queryable<TCompany>().AS("TCompanies").Select(it => new SelectListItem
            {
                Text = it.Name,
                Value = it.Code,
                Id = it.Id
            }).ToList();

            return new JsonResult(BaseResult.Result(result, "200"));

        }

        /// <summary>
        /// 根据字典编码获取字典列表
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetDicItemByCode/{code}")]
        public IActionResult GetDicItemByCode(string code)
        {
            var result = _Client.Queryable<TDictItem>().AS("TDictItems").Where(it => it.TypeCode == code)
             .Select(it => new SelectListItem
             {
                 Id = it.Id,
                 Text = it.ItemName,
                 Value = it.ItemCode
             }) 
             .OrderBy(it => it.Id)  
             .ToList();

            return new JsonResult(BaseResult.Result(result, "200"));
        }


    }
}
